package com.demo.project_template.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.demo.project_template.constant.GoldEnum;
import com.demo.project_template.entity.SignIn;
import com.demo.project_template.entity.User;
import com.demo.project_template.entity.UserGoldRecord;
import com.demo.project_template.exception.ErrorCodeException;
import com.demo.project_template.mapper.SignInMapper;
import com.demo.project_template.mapper.UserMapper;
import com.demo.project_template.service.SignInService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.project_template.service.UserGoldRecordService;
import com.demo.project_template.util.DateUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 杨玉龙
 * @since 2024-08-20 18:03:50
 */
@Service
public class SignInServiceImpl extends ServiceImpl<SignInMapper, SignIn> implements SignInService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private UserGoldRecordService userGoldRecordService;

    @Override
    public void signIn(Long userId) {
        User user = userMapper.selectById(userId);
        if ("1".equals(user.getIsSignIn())){
            throw new ErrorCodeException("今日已签到");
        }
        SignIn signIn = getOne(new LambdaQueryWrapper<SignIn>().eq(SignIn::getDaySort, DateUtil.todayOfWeek()));
        Integer goldCount = signIn.getGoldCount();
        UserGoldRecord userGoldRecord = new UserGoldRecord();
        userGoldRecord.setUserId(userId);
        userGoldRecord.setAmount(goldCount);
        userGoldRecord.setType(GoldEnum.SIGN_IN_ADD.getCode());
        userGoldRecord.setReason(GoldEnum.SIGN_IN_ADD.getName());
        userGoldRecord.setIsAdd("1");
        userGoldRecordService.add(userGoldRecord);
    }
}
